package com.team12.hotel404.controller.order;
import com.alipay.api.AlipayApiException;
import com.team12.hotel404.bean.AlipayBean;
import com.team12.hotel404.domain.Order;
import com.team12.hotel404.service.OrderService;
import com.team12.hotel404.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping(value = "/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Resource
    private PayService payService;//调用支付服务

    @RequestMapping(value = "/list")
    public String list_all_order(Model model) {
        List<Order> orders = orderService.listAllOrder();
        model.addAttribute("orders", orders);
        return "admin/order-list";
    }
    /* 订单接口 */



        /*阿里支付*/
        @PostMapping(value = "/alipay")
        public void alipay(@RequestParam("orderid") Integer orderId, @RequestParam("subject") String subject, @RequestParam("price") BigDecimal orderPayment, String body, HttpServletResponse response) throws AlipayApiException, IOException {
            String id = Integer.toString(orderId);
            String result = orderPayment.toString();
            StringBuffer buffer = new StringBuffer(result);
            AlipayBean alipayBean = new AlipayBean();
            alipayBean.setBody(subject);
            alipayBean.setOut_trade_no(id);
            alipayBean.setTotal_amount(buffer);
            alipayBean.setSubject(subject);

            String form = payService.aliPay(new AlipayBean().setBody(subject).setOut_trade_no(id).setTotal_amount(new StringBuffer().append(buffer)).setSubject(subject));

//            return payService.aliPay(new Order().setOrderId(orderId).setTotal_amount(new StringBuffer().append(orderPayment)).setSubject(subject));
           // String form =  payService.aliPay(new Order().setOrderDesc(subject).setOrderDesc(subject).setOrderPayment(orderPayment).setOrderDesc(subject));
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write(form);//直接将完整的表单html输出到页面
            response.getWriter().flush();
        }

}
